﻿using BootstrapBlazor.Components;
using Mapster;
using Microsoft.AspNetCore.Components;
using OnWork.DataCenter.BLL.DataCenter;
using OnWork.DataCenter.DAL.DataCenter;
using OnWork.DataCenter.Shared.Common;

namespace OnWork.DataCenter.Shared.Pages.DataCenter
{
    public partial class DBConnPage : ComponentBaseDataCenter
    {
        /// <summary>
        /// 依赖注入 提示信息
        /// </summary>
        [Inject]
        protected DBConnBLL dBConnBLL { get; set; }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private async Task<QueryData<DBConnPageVO>> QueryAsync(QueryPageOptions options)
        {
            var result = await dBConnBLL.QueryPagedListAsync(options.ToBaseQueryPageInput());
            //对象转换
            return result.ToQueryData<DBConnPageVO, DBConn>();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        private async Task<bool> OnDeleteAsync(IEnumerable<DBConnPageVO> item)
        {
            var result = await dBConnBLL.DeleteAsync(item.Select(x => x.id).ToArray());

            return await ShowSqlReturnMsg(result);
        }

        //保存数据时
        private async Task<bool> OnSaveAsync(DBConnPageVO item, ItemChangedType changedType)
        {
            //RESTfulResult<object> result;
            if (changedType == ItemChangedType.Update) // 更新
            {
                var result = await dBConnBLL.UpdateDatabaseAsync(item.Adapt<DBConn>());
                return await ShowSqlReturnMsg(result);
            }
            else //新增
            {
                var result = await dBConnBLL.AddDAsync(item.Adapt<DBConn>());
                return await ShowSqlReturnMsg(result);
            }
        }


        //保存数据时
        private async Task OnDatabaseValueChangedAsync(DBConnPageVO tt )
        {
            if (tt.type==SqlSugar.DbType.MySql)
            {
                tt.host = "mysql";
            }
            await Task.CompletedTask;
            
        }
    }
}